<?php
require_once('TVB/pms/pdes/action/InfoGetAction.class.php');

class InfoGeneralUpdateAction extends BaseAction 
{
	public $id;
	/**
	 * @maxLength 255
	 * @label Title (Chinese)
	 */
	public $title_chinese;
	/**
	 * @maxLength 255
	 * @label Title (English)
	 */
	public $title_english;
	/**
	 * @maxLength 255
	 * @label Type
	 */
	public $type;
	public $description;
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;
	public $episode_id;

    /**
	 * @filter datetime
	 * @label Publish Date/Time 
	 */
    public $publish_datetime;
    
    /**
	 * @filter datetime
	 * @label Expiry Date/Time
	 */
	public $expire_datetime;

	public $programme_id;
	public $onair_episode_no;

	public $ut;
	public $reset;
	public $from;
	public function execute(){
		if (!is_null($this->reset))
		{
			$this->setData('id', $this->id);
			$this->setData('type', 'general_info');
			$this->setData('act', 'edit');
			$this->setData('from', $this->from);
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if (CommonUtils::is_concurrent('info_general_information', $this->id, $this->modified_at))
			{
				$this->init_page_data();
				$this->addError('concurrent', array('0105', 'Concurrent update found!'));
				return 'logic-error';
			}

			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->title_chinese) && is_null($this->title_english))
			{
				$this->init_page_data();
				$this->addError('General Information.Title', array('0041', 'Title'));
				return 'filter-error';
			}
    
            if (!is_null($this->expire_datetime))
			{
				if ($this->publish_datetime > $this->expire_datetime)
				{
					$this->init_page_data();
					$this->addError('Publish Date/Time && Expiry Date/Time', array('0033', 'Publish Date/Time && Expiry Date/Time'));
					return 'filter-error';
				}
			}

			$update_id = $this->update_info_general_information();
			if ($update_id)
			{
				$this->setData('type','general_info');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return ($this->from == 'task') ? "task" : "succ";
		}
		catch(Exception $e)
		{
			error_log("Update general_info Fails: {$this->id}" . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

	/**
	 * update info_general_information
	 */
	private function update_info_general_information()
	{
		$info_general_information = new info_general_information();
		$info_general_information = $info_general_information->get($this->id);
		Model::diff($info_general_information, $this);
		$info_general_information->modified_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_general_information->modified_by = CommonUtils::get_user();
		$info_general_information->title_chinese = trim($this->title_chinese);
		$info_general_information->title_english = trim($this->title_english);
		$info_general_information->type  = trim($this->type);
        $info_general_information->publish_datetime = $this->publish_datetime;
        $info_general_information->expire_datetime  = $this->expire_datetime;
      
		$update_id = Model::$dbo['w']->save($info_general_information);
		if ($update_id > 0)
		{
			$info_general_information->add_change_log();

            $primary_key = array(
                'id'    => $this->id
            );
            $fields = array(
                'title_chinese',
                'title_english',
                'type',
                'description'
            );
            CommonUtils::filterC2A0('info_general_information', $primary_key, $fields);
			$this->addInfo("info_general_information:".AC::LOG_OP_UPDATE.":".$this->id, "update info_general_information success", json_encode($info_general_information));
			return true;
		}
		else
		{
			$this->addError("info_general_information:".AC::LOG_OP_UPDATE.":".$this->id, "update info_general_information failed", json_encode($info_general_information));
			return false;
		}
	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$action = new InfoGetAction();
		$action->id = $this->id;
		$action->act = 'edit';
		$action->type = 'general_info';
		$action->execute();

		$old_data = $action->getData();
		$this->modified_at = $old_data['general_info']->modified_at;
		$old_data['general_info'] = Model::diff($old_data['general_info'], $this);
		foreach ($old_data as $object=>$data)
		{
			$this->setData($object, $data);
		}

		//jump page
		$this->setData('id', $this->id);
		$this->setData('type', 'general_info');
		$this->setData('act', 'edit');
		$this->setData('from', $this->from);
	}
}
?>
